var nametxt = $('.slot');
var phonetxt = $('.name');
var appendTxt = $('.appendTxt1');
var pcount = phone.length - 1;//参加人数
var runing = true;
var trigger = true;
var num = 0;
var isReturn = false;
var LotterynumberArr= new Array();
LotterynumberArr['5-1'] = 20;
LotterynumberArr['5-2'] = 20;
LotterynumberArr['5-3'] = 20;
LotterynumberArr['5-4'] = 20;
LotterynumberArr['5-5'] = 20;
LotterynumberArr['4-1'] = 10;
LotterynumberArr['4-2'] = 10;
LotterynumberArr['4-3'] = 10;
LotterynumberArr['3'] = 10;
LotterynumberArr['2'] = 1;
LotterynumberArr['1'] = 1;
$(function () {
    nametxt.css('background-image', 'url(' + xinm[0] + ')');
    phonetxt.html(deptArr[0]+" "+phone[0]);
});

// 开始停止
function start() {
    if (isReturn) {
        alert("请返回重新设置中奖人数");
        return;
    }
    if (runing) {

        if (pcount < Lotterynumber) {
            alert("抽奖人数不足1人");
        } else {
            runing = false;
            $('#start').text('停止');
            startNum()
        }

    } else {
        $('#start').text('自动抽取中(' + Lotterynumber + ')');
        zd();
    }

}

// 开始抽奖

function startLuck() {
    runing = false;
    $('#btntxt').removeClass('start').addClass('stop');
    startNum()
}

// 循环参加名单
function startNum() {
    num = Math.floor(Math.random() * pcount);
    nametxt.css('background-image', 'url(' + xinm[num] + ')');
    phonetxt.html(deptArr[num]+" "+phone[num]);
    t = setTimeout(startNum, 100);
}

// 停止跳动
function stop() {
    pcount = phone.length - 1;
    clearInterval(t);
    t = 0;
}

// 打印中奖人

function zd() {
    if (isReturn) {
        alert("请返回重新设置中奖人数");
        return;
    }
    if (trigger) {

        trigger = false;
        if (pcount >= Lotterynumber) {
            stopTime = window.setInterval(function () {
                if (runing) {
                    runing = false;
                    $('#btntxt').removeClass('start').addClass('stop');
                    startNum();
                } else {
                    runing = true;
                    $('#btntxt').removeClass('stop').addClass('start');
                    stop();
					if($('.appendTxt1>li').length<10){
						appendTxt = $('.appendTxt1');
					}else{
						appendTxt = $('.appendTxt2');
					}
                    Lotterynumber--;

                    $('#start').text('自动抽取中(' + Lotterynumber + ')');

                    if (Lotterynumber == 0) {
                        console.log("抽奖结束");
                        window.clearInterval(stopTime);
                        $('#start').text("开始");
                        trigger = true;
                    }
                    //打印中奖者名单
                    appendTxt.prepend("<li><div class='portrait' style='background-image:url(" + xinm[num] + ")'></div><div class='luckuserName'>"+ phone[num] + "</div></li>");
					//将已中奖者放入中奖数组
					localPeople.unshift(deptArr[num]+" "+phone[num]);
					localStorage.setItem(localName, JSON.stringify(localPeople));
                    //将已中奖者从数组中"删除",防止二次中奖
                    xinm.splice($.inArray(xinm[num], xinm), 1);
                    phone.splice($.inArray(phone[num], phone), 1);
					localStorage.setItem("canYuChouJiangRen", JSON.stringify(phone));
                    isReturn = true;
                }
            }, 1000);
        }
    }
}

